package com.handsome.观察者模式;

import java.util.ArrayList;
import java.util.List;

/**
 * @author niechun
 * @date 2023/3/19 15:58
 */
public class WeatherDataSubject implements Subject{
    private String temperature;
    private String pressure;
    private String humidity;
    private List<Observer> list;

    public WeatherDataSubject() {
        list=new ArrayList<>();
    }

    public WeatherDataSubject(String temperature, String pressure, String humidity) {
        this.temperature = temperature;
        this.pressure = pressure;
        this.humidity = humidity;
    }

    public void changeData(String temperature, String pressure, String humidity){
        this.temperature = temperature;
        this.pressure = pressure;
        this.humidity = humidity;
        notifyObserver();
    }

    @Override
    public void register(Observer observer) {
        list.add(observer);
    }

    @Override
    public void remove(Observer observer) {
        if (list.contains(observer)){
            list.remove(observer);
        }
    }

    @Override
    public void notifyObserver() {
        for (Observer observer : list) {
            observer.update(temperature,pressure,humidity);
        }
    }
}
